package com.hs.app.catagory;
 
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hs.app.model.CdcTags;
import com.hs.app.service.ICatagoryService;
import com.hs.core.action.BaseAction;
import com.hs.core.db.DBMgr;
import com.hs.core.utils.PageParam;
import com.hs.core.utils.PageUtil;
@Controller   
public class CatagoryController  extends BaseAction {
    /**
	 * 
	 */
 	private static final Logger logger = Logger.getLogger(CatagoryController.class);
	private ICatagoryService catagoryService;
	@Autowired  
	public void setCatagoryService(ICatagoryService catagoryService) {
		this.catagoryService = catagoryService;
	}  
	/**
	  * 主题分类主页面
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/catagory/catagoryframe.do")   
    public ModelAndView catagoryframe(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 
		 return new ModelAndView("app/catagory/catagoryframe",modelMap);
	 }
	 /**
	  * 打开主题分类编辑页面
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/catagory/catagoryinfo.do")   
     public ModelAndView catagoryinfo(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 String cid = request.getParameter("cid");
		 CdcTags tag = null;
		 if(StringUtils.isNotEmpty(cid)){
			tag = (CdcTags)this.catagoryService.findByID(CdcTags.class, new Long(cid)); 
		 }
		 String sql = "select tagname,id from CDC_TAGS where pid=0 order by ordernum";
		 DBMgr dbo = this.getDbo(request);
		 List datalist = dbo.queryList(sql, null);
		 modelMap.put("catagorytype", datalist);
		 modelMap.put("cdcobj", tag);
		 return new ModelAndView("app/catagory/catagoryinfo",modelMap);
	 }
	 /**
	  * 左侧分类树
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/catagory/leftcatagorytree.do")   
     public ModelAndView leftcatagorytree(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 String sql = "select tagname,id from CDC_TAGS where pid=0 order by ordernum";
		 DBMgr dbo = this.getDbo(request);
		 List datalist = dbo.queryList(sql, null);
		 modelMap.put("treedata", datalist);
		 return new ModelAndView("app/catagory/treeinfo",modelMap);
	 }
	 
	 /**
	  * 数据加工列表
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/catagory/catagorylist.do")   
     public ModelAndView catagorylist(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 
		 PageParam pageParam = new  PageParam(request,"catagory/catagorylist.do");
		 PageUtil pageUtil = null;
		 String orderName = request.getParameter("orderName");
		 String tagtype = request.getParameter("tagtype");
		 String catagoryname = request.getParameter("catagoryname");
		 String orderNum = request.getParameter("orderNum");
		 
		 StringBuffer orderByStr = new StringBuffer(" order by ");
		 if(StringUtils.isNotBlank(orderName)){
		    	if("1".equals(orderNum)){
		    		orderByStr.append(orderName).append(" asc");
		    	}
		    	if("2".equals(orderNum)){
		    		orderByStr.append(orderName).append(" desc");
		    	}
		    }else{
		    	orderByStr.append("a.tagtype").append(" asc");
		    	orderName = "a.tagtype";
		    	orderNum = "2";
		    }
		 request.setAttribute("orderName", orderName); 
		 request.setAttribute("orderNum", orderNum); 
		 pageParam.getConditionMap().put("orderName", orderName);
		 pageParam.getConditionMap().put("orderNum", orderNum);
		 StringBuffer searchBuffer = new StringBuffer(" select a from CdcTags a where a.id>3   ");
		 String tagname = request.getParameter("tagname");   
		 request.setAttribute("tagname", tagname==null?"":tagname.trim());
		//request.setAttribute("periodicalname", periodicalname);
		 if(StringUtils.isNotBlank(tagname)){
			String temp = tagname.trim();
			searchBuffer.append(" and a.tagname like '%").append(temp).append("%' "); 
			pageParam.getConditionMap().put("tagname", temp); 
	   	 }  

		 searchBuffer.append(orderByStr);
		//Map data = new HashMap();
		 pageParam.setSearchSql(searchBuffer.toString());
		 pageUtil = catagoryService.getPageList(pageParam);
		
		 String sql = "select tagname,id from CDC_TAGS where pid=0 order by ordernum";
		 DBMgr dbo = this.getDbo(request);
		 List datalist = dbo.queryList(sql, null);
		 modelMap.put("catagorytype", datalist);
		 modelMap.put("tagtype", tagtype); 
		 modelMap.put("dataList", pageUtil);      
		 return new ModelAndView("app/catagory/catagorylist",modelMap);
	 } 
}
